window.onload = function(obj){
	console.log("AL")
	//---------热门推荐---------------------
	hotRecommendNodes = document.getElementsByClassName("keyword ");
	for(var i=0; i<hotRecommendNodes.length; i++){
		hotRecommendNodeBody = hotRecommendNodes[i];
		searchKey = hotRecommendNodeBody.innerText;
		Ajax.get("https://suggest.taobao.com/sug?extras=1&code=utf-8&callback=KISSY.Suggest.callback&q="+searchKey, function(data){
			re = /\[\[.*\]\]/;
			rs = JSON.parse(data.match(re)[0]);
			if(rs){
				total = 0;
				for(var i=0,item;i<rs.length,item=rs[i];i++){
					total += parseInt(item[1]);
				}
				hotRecommendNodeBody.title = searchKey + "(" + total + ")";
			}
		})
	}
	
	categoryNodes = document.getElementsByClassName("category-name");
	for(var i=0; i<categoryNodes.length; i++){
		categoryNodeBody = categoryNodes[i];
		searchKey = categoryNodeBody.innerText;
		Ajax.get("https://suggest.taobao.com/sug?extras=1&code=utf-8&callback=KISSY.Suggest.callback&q="+searchKey, function(data){
			re = /\[\[.*\]\]/;
			results = data.match(re);
			if(!results){
				console.log("数据不符合标准：" + results)
				return;
			}else{
				results = results[0];
			}
			rs = JSON.parse(results);
			if(rs){
				total = 0;
				for(var i=0,item;i<rs.length,item=rs[i];i++){
					total += parseInt(item[1]);
				}
				categoryNodeBody.title = searchKey + "(" + total + ")";
			}
		})
	}
	
	//------------获取店铺星级--------------------
	shopNodes = document.getElementsByClassName("shopname J_MouseEneterLeave J_ShopInfo");
	for(var i=0; i<shopNodes.length; i++){
		categoryNodeBody = shopNodes[i];
		userId = categoryNodeBody.getAttribute("data-userid")
		Ajax.get("https://s.taobao.com/api?sid="+ userId +"&callback=shopcard&app=api&m=get_shop_card", function(data){
			re = /\{.*\}/;
			results = data.match(re);
			if(!results){
				console.log("数据不符合标准：" + results)
				return;
			}else{
				results = results[0];
			}
			rs = JSON.parse(results);
			if(rs){
				for(var i=0,item;i<rs.levelClasses.length,item = rs.levelClasses[i];i++){
					nspan = document.createElement("span");
					nspan.className = item["levelClass"];
					categoryNodeBody.appendChild(nspan);
				}
			}
		})
	}
}

function createTag(type, src) {
	console.log(src)
	if (type !== 'link' && type !== 'script') {
		return;
	}
	var tag = document.createElement(type);
	if (type === 'link') {
		tag.rel = 'stylesheet';
		tag.href = src;
	} else if (type === 'script') {
		tag.type = 'text/javascript';
		tag.src = src;
	}
	return tag;
}


var Ajax={
    get: function(url, fn) {
        var xhr = new XMLHttpRequest();  // XMLHttpRequest对象用于在后台与服务器交换数据          
        xhr.open('GET', url, false);
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4 && xhr.status == 200 || xhr.status == 304) { // readyState == 4说明请求已完成
                fn.call(this, xhr.responseText);  //从服务器获得数据
            }
        };
        xhr.send();
    },
    post: function (url, data, fn) {         // datat应为'a=a1&b=b1'这种字符串格式，在jq里如果data为对象会自动将对象转成这种字符串格式
        var xhr = new XMLHttpRequest();
        xhr.open("POST", url, true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");  // 添加http头，发送信息至服务器时内容编码类型
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 304)) {  // 304未修改
                fn.call(this, xhr.responseText);
            }
        };
        xhr.send(data);
    }
}


